/*
 * tpy_doc.prg 
 * This file is part of <Tepuy Software> 
 *
 * Copyright (C) 2010 - Riztan Gutierrez  <riztan at gmail.com>
 *
 * <Tepuy Software> is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * <Tepuy Software> is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "gclass.ch"
#include "proandsys.ch"
#include "pc-soft.ch"
#include "hbclass.ch"

CLASS TPY_DOC //FROM GWIDGET

   DATA oConn          INIT  TPY_CONN
   DATA cSchema
   
   DATA nListBoxWidth 
   DATA nListBoxHeight
   DATA nFormWidth
   DATA nFormHeight
   
   DATA cListBoxTitle
   DATA cFormTitle
   
   DATA cQHead
   DATA aQHeadTables
   DATA cQHeadWhere
   DATA cQHeadOther
//   DATA aHeadLink         INIT {}
   DATA aHeadLinkCol      INIT {}
   
   DATA cListBoxResource
   DATA cFormResource
   
   DATA cQDetal
   DATA aQDetalTables
   DATA cQDetalWhere
   DATA cQDetalOther
   DATA aDetalLink         INIT {}
//   DATA aDLinkCol      INIT {}

   DATA bLink 

   DATA oHeadModel
   DATA oDetalModel
   DATA oListBox

   METHOD New(oConn, cSchema, lRetValue, cFieldRet)
   METHOD Edit( oModel, nPos ) 
   METHOD HDLink( cField )     INLINE  Eval( ::bLink, ::oHeadModel, cField)
   METHOD SetHModel( cQuery )
   METHOD SetQueryDetal()
   METHOD Active()
   
ENDCLASS


METHOD New( oConection, cSchema, lRetValue, cFieldRet, cQHead, ;
            cListBoxTitle, nLWidth, nLHeight, cListBoxResource, ;
            cFormTitle, nFWidth, nFHeight, cFormResource ) CLASS TPY_DOC
   
   Local oModel, oListBox
   Local cQuery, uRes:=" "
   Local _cSchema
   
   Default cSchema := "tepuy", lRetValue := .f., cFieldRet:=""
   Default nLWidth := 550, nLHeight := 300
   Default nFWidth := 450, nFHeight := 500
   Default cListBoxTitle := "", cFormTitle := ""
   Default cFormResource := "simple_form_03.glade"
   
   ::nListBoxWidth  := nLWidth
   ::nListBoxHeight := nLHeight
   
   ::nFormWidth  := nFWidth
   ::nFormHeight := nFHeight
   
   ::cListBoxTitle := cListBoxTitle
   ::cFormTitle    := cFormTitle
   
   ::cListBoxResource := cListBoxResource
   ::cFormResource    := cFormResource
   
   If HB_ISNIL(oConection)
      ::oConn  := TPY_CONN
   EndIf
   
   ::cSchema := cSchema
   _cSchema  := ::oConn:Schema

   ::bLink := {|o,field| IIF( o:lListore, DataToSQL( o:GetCol( field ) ), "''") }


   ::oConn:SetSchema(::cSchema)

   If !HB_ISNIL(cQHead)
      cQuery := cQHead
   EndIf

   ::SetHModel( cQuery )



//   ACTIVATE LISTBOX ::oListBox


   //::oConn:SetSchema(_cSchema)

Return Self


/*
 *
 */
METHOD SetHModel( cQuery ) CLASS TPY_DOC

   ::oHeadModel := NIL
   ::oListBox   := NIL

   // Definiendo el Modelo
   DEFINE MODEL ::oHeadModel ;
          CONN  ::oConn  ;
          QUERY cQuery 
          
   // Definiendo el ListBox
   DEFINE LISTBOX ::oListBox                       ;
          MODEL   ::oHeadModel                     ;
          TITLE   ::cListBoxTitle                  ;
          SIZE    ::nListBoxWidth,::nListBoxHeight ;   

//-- puedes usar glade y personalizas tu edición.
//          ID "listbox" RESOURCE "listbox.glade"

        ::oListBox:bNew  := {|| ::Edit( ::oHeadModel ) }
        ::oListBox:bEdit := {|| ::Edit( ::oHeadModel, ::oHeadModel:GetPosRow() ) }
      
/*      
    If lRetValue
       ::oListBox:lBar := .f.
       ::oListBox:oWnd:SetSkipTaskBar( .T. ) // No queremos que salga en la barra de tareas       
       ::oListBox:bEdit := {|| uRes:= ::oHeadModel:GetCol( "Identificación" ),;
                               ::oListBox:End() }
    EndIf      
*/

Return Self


/*
 *
 */
METHOD SetQueryDetal()   CLASS TPY_DOC

   Local cQuery:=""

   IF Len(::aDetalLink)!=Len(::aHeadLinkCol)
      Return cQuery
   ENDIF   
   
   cQuery := ::cQDetal+" from "
   AEVAL( ::aQDetalTables,{|link,n| cQuery += IIF( n>1 , ", " , "" )+;
                             link } )
   cQuery += " where "

   AEVAL( ::aDetalLink,{|link,n| cQuery += IIF( n>1 , " and " , "" )+;
                             link+"="+::HDLink(::aHeadLinkCol[n]) } ) 
                             
   cQuery += " "+::cQDetalOther

Return cQuery


/*
 *
 */
METHOD ACTIVE() CLASS TPY_DOC

   ACTIVATE LISTBOX ::oListBox
   
RETURN NIL



/*
 *  Funcion de Edición
 */ 
//Function Edit( oModel, nPos )
METHOD Edit( oModel, nPos ) CLASS TPY_DOC
   Local oForm, oWnd, oBox, oBoxData
   Local cResource
   Local cQuery, oQuery
   Local oListBox

   Default nPos:=0
   
   cQuery := ::SetQueryDetal() 

   DEFINE MODEL ::oDetalModel ;
          CONN ::oConn;   
          QUERY cQuery
          
//   oDetalModel:oConn:StartTransaction()
          
   SET RESOURCES cResource FROM FILE oTepuy:cResources+::cFormResource
   
   DEFINE WINDOW oWnd TITLE ::cFormTitle ;
          SIZE ::nFormWidth,::nFormHeight ID "window1" RESOURCE cResource
   
   DEFINE BOX oBox ID "hbox1" RESOURCE cResource
   
      DEFINE BUTTON oBtnGuardar ;
             ACTION ( oForm:lAcepta:=.t., IIF(oForm:Save(),oWnd:End(),NIL) );
             ID "btnguardar" RESOURCE cResource

      DEFINE BUTTON oBtnCerrar  ;
             ACTION ( oWnd:End() );
             ID "btncerrar"  RESOURCE cResource
   
   
      DEFINE FORMEDIT  oForm ;
             MODEL ::oHeadModel ROW nPos ;
             OF oBox ;
          
             //-- Activas/Desactivas la imagen
             oForm:lImage := .F.

             oForm:bSave := {|o| !Empty( o:GetValue("class_id") ) }
          

      DEFINE BOX oBoxData ID "vboxdata" RESOURCE cResource

      DEFINE LISTBOX oListBox                ;
             MODEL   ::oDetalModel           ;
             OF oBoxData 

/*
         ::oDetalModel:cPreQryUpdate := " and clsdata_class_id="+;
                                DataToSQL( ::oHeadModel:GetCol( "Identificación" ) )

         ::oDetalModel:aPreQryInsert := {"clsdata_class_id",;
                                    DataToSQL( ::oHeadModel:GetCol( "Identificación" ))}
*/                                 
         oListBox:oBarButton:SetStyle( GTK_TOOLBAR_ICONS )
   
         oListBox:bNew  := {|| EditData( oForm,::oDetalModel ) }
         oListBox:bEdit := {|| EditData( oForm,::oDetalModel, ::oDetalModel:GetPosRow() ) }

//             oListBox:lBar := .f.

      ACTIVATE LISTBOX oListBox

      //-- Ocultamos los botones: imprimir y salir.
      oListBox:oBtns:oTBtnQuit:Hide()
      oListBox:oBtns:oTBtnPrint:Hide()
          
   ACTIVATE FORMEDIT oForm
   

   ACTIVATE WINDOW oWnd

Return .T.



/*
 *  Funcion de Edición de Detalles
 */
Function EditData( oHead, oModel, nPos )

   Local oForm, oWnd, oBox
   Local nAlto:= 200, nAncho := 400
   Local cResource

   Default nPos:=0
   
   If Empty(oHead:GetValue("class_id") ) .OR. ;
      Empty(oHead:GetValue("class_name"))
      Return .F.
   EndIf

   //Actualizamos valores por defecto.
   oModel:cPreQryUpdate := " and clsdata_class_id="+;
                           DataToSQL( oHead:GetValue( "class_id" ) )

   oModel:aPreQryInsert := {"clsdata_class_id",;
                           DataToSQL( oHead:GetValue( "class_id" ) )}

   
   DEFINE FORMEDIT  oForm ;
          TITLE "Item de Clasificador" ;
          SIZE nAncho,nAlto ;
          MODEL oModel ROW nPos ;
          
          // -- se puede usar glade
          //ID "window1" RESOURCE "simple_form_01.glade"
          
          //-- Activas/Desactivas la imagen
          oForm:lImage := .F.
//          oForm:lFix   := .F.
          
   ACTIVATE FORMEDIT oForm
   
Return .T.

//EOF
